# -*- coding: utf-8 -*-

import traceback
        
def db_execute(connect, sql_str):
    """执行sql_str语句返回cursor"""
    try:
        cursor = connect.cursor()
        cursor.execute(sql_str)
        return cursor
    except Exception as e:
        print("error:", traceback.format_exc(e))
    finally:
#        connect.close()
        pass
    pass


def db_fetchone(cursor):
    """返回单条记录（元组）"""
    try:
        result_tuple = cursor.fetchone()
        return result_tuple
    except Exception as e:
        print("error:", traceback.format_exc(e))
        pass
    finally:
        cursor.close()
        pass
    pass

def db_fetchall(cursor):
    """返回所有记录（列表）"""
    try:
        result_list = list(cursor.fetchall())
#        result_list = []
#        for record_tuple in cursor.fetchall():
#            result_list.append(record_tuple)
#            pass
        return result_list
    except Exception as e:
        print("error:", traceback.format_exc(e))
        pass
    finally:
        cursor.close()
        pass
    pass